﻿Imports System.ComponentModel.Composition

<Export(GetType(IInfoSender))>
<ExportMetadata("SenderName", "Twitter")>
Public Class TwitterSender
    Inherits InfoSenderBase

    Friend Shared TwiSender As TwitterOAuth

    Public Overrides Function GetAvailability() As Boolean
        If My.Settings.UseOAuth Then
            Return HaveToken()
        Else
            Return True
        End If
    End Function

    Public Overrides Sub SendInfo(ByVal message As String)
        Try
            TwiSender.Update(message)
        Catch ex As Exception
            OnError(New Exception("无法发送" + Environment.NewLine + message, ex), True)
        End Try
    End Sub

    Public Overrides Sub ShowSetting()
        Static wr As WeakReference
        Dim sf As SettingForm

        Try
            If wr IsNot Nothing AndAlso wr.IsAlive Then
                sf = wr.Target
            Else
                sf = New SettingForm
                wr = New WeakReference(sf)
            End If
            sf.Show()

            sf = Nothing
        Catch ex As Exception
            Dim IsHandled As Boolean = False
            OnError(New ApplicationException("打开设置时发生问题！", ex), IsHandled)
            If Not IsHandled Then
                Throw
            End If
        End Try

        GC.Collect()
    End Sub

    Public Overrides Sub ShutDown()
        TwiSender = Nothing
        My.Settings.Save()
    End Sub

    Public Overrides Sub StartUp()
        Try
            TwiSender = New TwitterOAuth("8XgFJdid8MoXezExn1ojuw", "PzNd15Qc5C6TDzWrjNo5WYiGFl5SLoQZ79tSbvnOMI")
            If HaveToken() Then
                TwiSender.AddToken(My.Settings.Token, My.Settings.TokenSecret)
            End If
        Catch ex As Exception
            Dim IsHandled As Boolean = False
            OnError(New ApplicationException("实例化对象失败", ex), IsHandled)
            If Not IsHandled Then
                Throw
            End If
        End Try
    End Sub
End Class


